<template>
  <Modal
    v-model="showCopyTestsuiteModal"
    title="复制场景"
    :mask-closable="false"
    @on-cancel="handleCopyTestsuiteCancel"
  >
    <Form
      :label-width="80"
      ref="testsuiteForm"
      :model="testsuiteForm"
      :rules="testsuiteValidate"
    >
      <FormItem label="场景名称" prop="name">
        <Input clearable v-model="testsuiteForm.name" />
      </FormItem>
    </Form>
    <div slot="footer">
      <Button type="default" @click="handleCopyTestsuiteCancel">取消</Button>
      <Button type="primary" @click="handleCopyTestsuiteSubmit('testsuiteForm')"
        >确定</Button
      >
    </div>
  </Modal>
</template>
<script>
import { copyTestsuite } from "@/api/HttpAutoTestService/testsuite";
export default {
  name: "testsuite-copy",
  props: {
    testsuite: {
      type: Object,
      default: () => {
        return {};
      },
    },
    copyTestsuite: {
      type: Boolean,
      default: false,
    },
  },
  computed: {
    showCopyTestsuiteModal: {
      get() {
        return this.copyTestsuite;
      },
      set(val) {
        this.$emit("update:copyTestsuite", val);
      },
    },
  },
  watch: {
    testsuite(val) {
      if (this.showCopyTestsuiteModal && val) {
        this.testsuiteForm = { ...val };
      }
    },
  },
  data() {
    return {
      testsuiteForm: {
        id: "",
        name: "",
      },
      testsuiteValidate: {
        name: [{ required: true, message: "请输入场景名称", trigger: "blur" }],
      },
    };
  },
  methods: {
    handleCopyTestsuiteSubmit(name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          copyTestsuite(this.testsuiteForm)
            .then((res) => {
              if (res.status === 201) {
                this.$Message.info("创建成功");
                this.handleCopyTestsuiteCancel();
              } else {
                this.$Message.error("创建失败");
              }
            })
            .catch((err) => {
              this.$Modal.error({
                title: "系统异常，请联系管理员……",
                content: JSON.stringify(err),
                closable: true,
              });
            });
        }
      });
    },
    handleCopyTestsuiteCancel() {
      this.showCopyTestsuiteModal = false;
      this.testsuiteForm = {
        id: "",
        name: "",
      };
      this.$emit("update:testsuite", {});
      this.$emit("initTestsuiteTable");
      this.$refs['testsuiteForm'].resetFields();
    },
  },
};
</script>